package com.chesnutconsulting.otbasket.util;

import com.google.common.collect.Lists;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * User: kandhari
 * Date: 3/16/13 7:53 PM
 * <p/>
 * Document me
 */
public class FileUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(FileUtil.class);

    public static final List<File> getFilesFromDir(String dirPath) {
        List<File> files = Lists.newArrayList();
        File dir = new File(dirPath);
        if (dir.exists() && dir.isDirectory()) {
            for(File toAdd : dir.listFiles()) {
                if(toAdd.isFile()) {
                    files.add(toAdd);
                }
            }
        }

        return files;
    }

    public static final List<File> getFilesFromDir(String dirPath, String withExtension) {
        List<File> files = Lists.newArrayList();
        File dir = new File(dirPath);
        if (dir.exists() && dir.isDirectory()) {
            for(File toAdd : dir.listFiles()) {
                if(toAdd.isFile()
                        && withExtension.equalsIgnoreCase(getExtension(toAdd.getName()))) {
                    files.add(toAdd);
                }
            }
        }

        return files;
    }

    public static void clearDirectory(File dir) {
        if (dir.isDirectory()) {
            for (File file : dir.listFiles()) {
                if(!file.delete()) {
                    LOGGER.warn("Could not delete file={}. Please delete manually.", dir.getPath());
                };
            }
        }
    }

    public static File copy(String outputPath, File from) {
        FileOutputStream toStream = null;
        FileInputStream fromStream = null;
        try {
            File to = new File(outputPath);
            toStream = new FileOutputStream(to);
            fromStream = new FileInputStream(from);
            IOUtils.copy(fromStream, toStream);
            return to;
        } catch (IOException ioe) {
            LOGGER.error(String.format("Error copying file to path=%s", outputPath));
            throw new AssertionError("Could not copy file");
        } finally {
            IOUtils.closeQuietly(toStream);
            IOUtils.closeQuietly(fromStream);
        }
    }

    public static String getExtension(String filename) {
        if (filename == null) {
            return null;
        }
        int index = indexOfExtension(filename);
        if (index == -1) {
            return null;
        } else {
            return filename.substring(index+1, filename.length());
        }
    }

    public static String removeExtension(String filename) {
        if (filename == null) {
            return null;
        }
        int index = indexOfExtension(filename);
        if (index == -1) {
            return filename;
        } else {
            return filename.substring(0, index);
        }
    }

    public static int indexOfLastSeparator(String filename) {
        if (filename == null) {
            return -1;
        }
        int lastUnixPos = filename.lastIndexOf(UNIX_SEPARATOR);
        int lastWindowsPos = filename.lastIndexOf(WINDOWS_SEPARATOR);
        return Math.max(lastUnixPos, lastWindowsPos);
    }

    public static int indexOfExtension(String filename) {
        if (filename == null) {
            return -1;
        }
        int extensionPos = filename.lastIndexOf(EXTENSION_SEPARATOR);
        int lastSeparator = indexOfLastSeparator(filename);
        return lastSeparator > extensionPos ? -1 : extensionPos;
    }

    public static final char EXTENSION_SEPARATOR = '.';
    private static final char UNIX_SEPARATOR = '/';
    private static final char WINDOWS_SEPARATOR = '\\';
}
